﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class printYCbill : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] == null || Session["roleID"] == null)
        {
            Response.Redirect("Login.aspx");
            Response.End();
        }
        if (!IsPostBack)
        {
            string YCID = Request.QueryString["ycid"].ToString();
            string userName = Session["userName"].ToString();
            string roleID = Session["roleID"].ToString();
            BasicClass BasicC = new BasicClass();
            int userDepID = Convert.ToInt32(BasicC.GetDepIDByUserName(userName));
            YCDeal YCD = new YCDeal();
            if (roleID != "1" && !CheckRight(userName, YCID))
            {
                Response.Write("<script language='javascript'>alert('对不起！您无权打印异常处理单！');location.href='CheckYCList.aspx'</script>");
                Response.End();
            }
            else
            {
                BindYCData(YCID);
                BindToRpYCChekBill(YCID);

            }
        }

    }
    /// <summary>
    /// 判断用户是否有权查看当前异常
    /// </summary>
    /// <param name="userName">当前用户名</param>
    /// <param name="YCID">异常编号</param>
    /// <returns></returns>
    protected Boolean CheckRight(string userName, string YCID)
    {
        BasicClass BaseC = new BasicClass();
        int userDep = Convert.ToInt32(BaseC.GetDepIDByUserName(userName));
        YCDeal YCClass = new YCDeal();
        string crrentOperator = YCClass.GetCurrentOperator(YCID).ToLower();
        int YCUpDep = Convert.ToInt32(YCClass.GetUpYCDepIDByYCID(YCID).ToString());
        if (userDep == YCUpDep || crrentOperator == userName.ToLower() || YCClass.checkViewRight(YCID, userDep))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /// <summary>
    /// 绑定显示数据
    /// </summary>
    protected void BindYCData(string YCID)
    {
        string YCId = YCID;
        string dataYear = YCId.Substring(2, 4);
        string tabName = "YCMS" + dataYear;
        string sqlString = "select * from " + tabName + " where YCID='" + YCId + "'";
        DBOperation DBO = new DBOperation();
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        if (sdr.Read())
        {
            lblYCID.Text = YCId;
            lblDepName.Text = BasicClass.GetDepNameByDepID(sdr["DEPID"].ToString());
            string upUser = BasicClass.GetTrueNameByUserName(sdr["USERNAME"].ToString());
            lblUpUserName.Text = upUser;
            lblUpUser.Text = upUser;
            DateTime addDate = Convert.ToDateTime(sdr["ADDDATE"].ToString());
            lblAddDate.Text = addDate.ToShortDateString();
            lblAddDate2.Text = addDate.ToShortDateString();
            lblYear.Text = addDate.Year.ToString();
            //lblCurrentOperator.Text = sdr["CURRENTOPERATER"].ToString();
            lblContent.Text = sdr["CONTENT"].ToString();
            if (sdr["STATUS"].ToString() == "1")
            {
                //btnSubmit.Enabled = false;

            }
        }
        //绑定相关附件
        string lblFilesText = "";
        string fileName = "";
        string filePath = "";
        Hashtable annexFiles = GetAnnexFiles(YCId);
        for (int i = 0; i < annexFiles.Count; i++)
        {
            if (annexFiles[i].ToString() != "")
            {
                fileName = annexFiles[i].ToString();
                fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                filePath = annexFiles[i].ToString();
                int item = i + 1;
                filePath = Server.UrlEncode(filePath);
                lblFilesText += item.ToString() + "、<img src='images/filePic.gif' /><a href='DownLoad.aspx?filepath=" + filePath + "' target='blank'>" + fileName + "</a>,&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }
        sdr.Close();
        lblAnnexFiles.Text = lblFilesText;

    }
    protected Hashtable GetAnnexFiles(string YCID)
    {
        Hashtable annexFiles = new Hashtable();
        string dataYear = YCID.Substring(2, 4);
        string tableName = "YCFILES" + dataYear;
        string sqlString = "select * from " + tableName + " where YCID='" + YCID + "'";
        DBOperation DBO = new DBOperation();
        SqlDataReader sdr = DBO.GetDataReader(sqlString);

        for (int i = 0; sdr.Read(); i++)
        {
            annexFiles[i] = sdr["FILENAME"].ToString();
        }
        sdr.Close();
        return annexFiles;
    }
    protected void BindToRpYCChekBill(string YCID)
    {
        string YCId = YCID;
        string currentYear = YCId.Substring(2, 4);
        string tabName = "YCSTEPS" + currentYear;
        DBOperation DBO = new DBOperation();
        string sqlstring = "select * from " + tabName + " where YCID='" + YCId + "' and SEQ<>'1' order by SEQ ASC,AUTOID ASC";
        DataSet ds = DBO.GetDataSet(sqlstring, "TEMP");
        rpYCCheckBill.DataSource = ds.Tables["TEMP"].DefaultView;
        rpYCCheckBill.DataBind();

    }
}
